package cn.hnbjx.prepaid.service;

import cn.hnbjx.prepaid.common.BusinessException;
import cn.hnbjx.prepaid.common.BusinessStatus;
import cn.hnbjx.prepaid.dao.AdminDao;
import cn.hnbjx.prepaid.pojo.Admin;
import cn.hnbjx.prepaid.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminService {
    @Autowired
    private AdminDao adminDao;
    public Admin login(String name, String password) {
        System.out.println(MD5Util.getMD5(password));
        Admin user = adminDao.findUser(name, MD5Util.getMD5(password));
        System.out.println(user);
        if (null == user) {throw new BusinessException(BusinessStatus.ADMIN_PASSWORD); }
        return user;
    }
    public int updatePassword(Integer id, String oldPassword, String newPassword) {
        //校验参数有效性
        if(oldPassword == null)
            throw new BusinessException(BusinessStatus.RESOURCE_NOT_EXISTS,"原密码为空");
        System.out.println(oldPassword);
        if(newPassword == null)
            throw new BusinessException(BusinessStatus.RESOURCE_NOT_EXISTS,"新密码为空");
        //更新密码
        int rows=adminDao.updatePassword(MD5Util.getMD5(newPassword),id);
        if (rows == 0){
            throw new BusinessException(BusinessStatus.FAIL);
        }
        return rows;
    }
}
